package Zoo.View;

/*
*  重要！！（配套LoginHandler ）
* */


import Zoo.Handler.LoginHandler;
import Zoo.Impl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Loginview extends JFrame implements ItemListener {

    private int flag=0;  //用来标记登录者所选择的身份



    JLabel nameLabel=new JLabel("登录界面",JLabel.CENTER);

    SpringLayout springLayout =new SpringLayout();
    JPanel centerPanel =new JPanel(springLayout);
    JLabel userNameLabel =new JLabel("用户名:");
    JTextField usertxt =new JTextField();
    JLabel pwdLabel=new JLabel("密码/身份证号:");
    JPasswordField pwdField =new JPasswordField();
    JButton loginBtn =new JButton("登录");
    JButton resetBtn=new JButton("重置");
    JButton registerBtn=new JButton("注册");

    LoginHandler loginHandler;
    UserServiceImpl  userServiceImpl;
    //    JtableDemo JtableDemo;
//    AdminEntity adminEntity;
    private JFrame jf = new JFrame();
    // 创建四个单选按钮
    private JRadioButton radioBtn01 = new JRadioButton("用户");
    private JRadioButton radioBtn02 = new JRadioButton("参观者");
    private JRadioButton radioBtn03 = new JRadioButton("园长");
    private JRadioButton radioBtn04 = new JRadioButton("动物饲养员");
    public Loginview(){
        super("登录界面");
        loginHandler=new LoginHandler(this);
//        userServiceImpl=new UserServiceImpl(this);
 //       JtableDemo=new JtableDemo(this);
//        adminEntity=new AdminEntity(this);
        Container contentPane = getContentPane();



        nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        usertxt.setPreferredSize(new Dimension(200,30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200,30));
        loginBtn.setFont(centerFont);
        resetBtn.setFont(centerFont);
        registerBtn.setFont(centerFont);


        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(usertxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        loginBtn.addActionListener(loginHandler);
        //增加按键事件
        loginBtn.addKeyListener(loginHandler);
        centerPanel.add(loginBtn);
        resetBtn.addActionListener(loginHandler);
        centerPanel.add(resetBtn);
        registerBtn.addActionListener(loginHandler);
        centerPanel.add(registerBtn);

        //弹簧布局
        layourCenter();


        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);

        //界面大小
        setSize(500,300);
        setLocationRelativeTo(null);
        //关闭即为退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置不可改变大小
        setResizable(false);
        //设置可见
        setVisible(true);

        //设置LoginBtn为默认按钮
        getRootPane().setDefaultButton(loginBtn);


        // 创建按钮组，把两个单选按钮添加到该组
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(radioBtn01);
        btnGroup.add(radioBtn02);
        btnGroup.add(radioBtn03);
        btnGroup.add(radioBtn04);
        //单选按钮设置监听器
        radioBtn01.addItemListener(this);
        radioBtn02.addItemListener(this);
        radioBtn03.addItemListener(this);
        radioBtn04.addItemListener(this);
        // 设置第一个单选按钮选中
//        radioBtn01.setSelected(true);

        centerPanel.add(radioBtn01);
        centerPanel.add(radioBtn02);
        centerPanel.add(radioBtn03);
        centerPanel.add(radioBtn04);


        //监听窗体关闭事件
        this.jf.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent obj)
            {
                System.exit(1);
            }
        });
        }


    private void layourCenter() {
        //布局userNameLabel
        Spring childWidth  =Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(usertxt)),Spring.constant(20));
        int offsetx=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetx,SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);

        //布局usertxt
        springLayout.putConstraint(SpringLayout.WEST,usertxt,20,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,usertxt,0,SpringLayout.NORTH,userNameLabel);

        //布局pwdLabel
        springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,20,SpringLayout.SOUTH,userNameLabel);

        //布局pwdField
        springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel);

        //布局loginBtn
        springLayout.putConstraint(SpringLayout.WEST,loginBtn,130,SpringLayout.WEST,pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,pwdLabel);

        //布局resetBtn
        springLayout.putConstraint(SpringLayout.WEST,resetBtn,50,SpringLayout.EAST,loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn);

        //布局registerBtn
        springLayout.putConstraint(SpringLayout.WEST,registerBtn,150,SpringLayout.EAST,loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH,registerBtn,50,SpringLayout.NORTH,loginBtn);

        //radioBtn01
        springLayout.putConstraint(SpringLayout.WEST,radioBtn01,-200,SpringLayout.EAST,loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH,radioBtn01,40,SpringLayout.NORTH,loginBtn);

        //radioBtn02
        springLayout.putConstraint(SpringLayout.WEST,radioBtn02,30,SpringLayout.EAST,radioBtn01);
        springLayout.putConstraint(SpringLayout.NORTH,radioBtn02,0,SpringLayout.NORTH,radioBtn01);
        //radioBtn03
        springLayout.putConstraint(SpringLayout.WEST,radioBtn03,30,SpringLayout.EAST,radioBtn02);
        springLayout.putConstraint(SpringLayout.NORTH,radioBtn03,0,SpringLayout.NORTH,radioBtn02);
        //radioBtn04
        springLayout.putConstraint(SpringLayout.WEST,radioBtn04,30,SpringLayout.EAST,radioBtn03);
        springLayout.putConstraint(SpringLayout.NORTH,radioBtn04,0,SpringLayout.NORTH,radioBtn03);

    }

    public static void main(String[] args){
            new Loginview();
    }

    public JTextField getUsertxt() {
        return usertxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

//    public JButton getRegisterBtn() {
//        return registerBtn;
//    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == radioBtn01) {
//            radioBtn01.setIcon(new ImageIcon(male));
//            radioBtn01.setIcon(null);

            flag=1;
            System.out.println("用户");

        } else if(e.getSource() == radioBtn02) {
            flag=2;
            System.out.println("参观者");
//            jradiofemale.setIcon(new ImageIcon(female));
//            jradionmale.setIcon(null);
        }else if(e.getSource() == radioBtn03){
            flag=3;
            System.out.println("园长");
        }else if(e.getSource() == radioBtn04){
            flag=4;
            System.out.println("动物饲养员");
        }
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }
}
